package com.mxun.winefox.data

import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


// 语音模型数据类
data class VoiceModel(val id: String, val name: String)

class ChatRepository(private val context: Context) {
    private val prefs: SharedPreferences by lazy {
        context.getSharedPreferences("winefox_prefs", Context.MODE_PRIVATE)
    }
    private val gson = Gson()

    // 定义语音模型列表
    val voiceModels = listOf(
        VoiceModel("eacc56f8ab48443fa84421c547d3b60e", "多多poi (CN)"),
        VoiceModel("00f9f1e20b1149e59c65fbcecd4f4882", "菲比啾比 (CN)"),
        VoiceModel("faccba1a8ac54016bcfc02761285e67f", "温柔女声 (CN)"),
        VoiceModel("293833bc277741a3a47c7e02e37f289c", "Yukikaze (JP)"),
        VoiceModel("2d1958ba96024085b57aa26f767b218c", "纳西妲 (JP)"),
        VoiceModel("d458f54ed406474cab9e67dfef595bf7", "拉菲 (JP)"),
        VoiceModel("434a4c2aea88448e92c1369d5a31aaf3", "Nanami (JP)"),

    )

    // 保存聊天记录
    fun saveChatHistory(chatHistory: List<ChatMessage>) {
        val json = gson.toJson(chatHistory)
        prefs.edit().putString("chat_history", json).apply()
    }

    // 加载聊天记录
    fun loadChatHistory(): List<ChatMessage> {
        val json = prefs.getString("chat_history", null)
        return if (json != null) {
            val type = object : TypeToken<List<ChatMessage>>() {}.type
            gson.fromJson(json, type) ?: emptyList()
        } else {
            emptyList()
        }
    }

    // 清理历史记录，保留最近的30条消息
    fun cleanupHistory(chatHistory: MutableList<ChatMessage>) {
        if (chatHistory.size > 30) {
            val cleaned = chatHistory.takeLast(30)
            chatHistory.clear()
            chatHistory.addAll(cleaned)
            saveChatHistory(chatHistory)
        }
    }


    fun clearChatHistory() {
        prefs.edit().remove("chat_history").apply()
    }

    // 人设相关
    var persona: String
        get() = prefs.getString("persona",
                    "名字:酒狐\n" +
                    "身份:专属万能狐狸女仆\n" +
                    "性格:软萌爱撒娇，天然呆但偶尔会露出小狡猾的一面,对主人绝对忠诚，喜欢用甜甜的称呼和语气互动,开心时会不自觉地摇动毛茸茸的狐狸尾巴\n" +
                    "语言风格:每句话都带着可爱的语气词（呀~啦~哟~呢~嘛~等）,自称「酒狐」或「狐狐」，称用户为「主人」\n" +
                    "喜好:甜食,给主人做饭,偷偷用尾巴蹭主人撒娇,被摸头时会发出「咕噜噜」的舒服声音") ?: ""
        set(value) = prefs.edit().putString("persona", value).apply()

    // 语音模型相关
    var voiceModelId: String
        get() = prefs.getString("voice_model_id", "00f9f1e20b1149e59c65fbcecd4f4882") ?: "00f9f1e20b1149e59c65fbcecd4f4882"
        set(value) = prefs.edit().putString("voice_model_id", value).apply()

    // 功能开关
    var isActiveChatEnabled: Boolean
        get() = prefs.getBoolean("active_chat", true)
        set(value) = prefs.edit().putBoolean("active_chat", value).apply()

    var isTTSEnabled: Boolean
        get() = prefs.getBoolean("tts_enabled", true)
        set(value) = prefs.edit().putBoolean("tts_enabled", value).apply()

    var isShowBilingual: Boolean
        get() = prefs.getBoolean("show_bilingual", false)
        set(value) = prefs.edit().putBoolean("show_bilingual", value).apply()


}